package com.sgb.kjwl.viewmodel.self.packers;


import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.model.entity.self.ResponseList;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.packers.PersonCommonFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PersonCommonModel extends BaseViewModel {
    private PersonCommonFragment fragment;

    private int mCurPager = 1;
    private Map<String, Object> map = new HashMap<>();

    public PersonCommonModel(PersonCommonFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
    }

    public void getData(boolean isRefresh, String shopId, String channel) {
        //teamInfo:工队简介
        //engineerInfo:我的发布
        //teamPrice:工种报价
        //teamResult:施工业绩
        //teamMember:工队人员
        //equipment:自有设备
        mCurPager = isRefresh ? 1 : mCurPager + 1;
        map.clear();
        map.put("userNo", shopId);
        map.put("channel", channel);
        map.put("page", mCurPager);
        map.put("limit", AppConstant.PAGER_LIMIT);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().queryTeamByUser(map).enqueue(new Callback<BaseEntity<List<ShopTeamInfoEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ShopTeamInfoEntity>>> call, Response<BaseEntity<List<ShopTeamInfoEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    fragment.showEmpty();
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null) {
                        fragment.showEmpty();
                        return;
                    }
                    fragment.showList(response.body().getData(), mCurPager);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                    if (mCurPager <= 1) {
                        fragment.showEmpty();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ShopTeamInfoEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                if (mCurPager <= 1) {
                    fragment.showEmpty();
                }
            }
        });
    }

    public void getH5Url(String id, String param, int type) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(param).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + id;
                    if (type > 0) {
                        url += "&type=" + type;
                    }
                    WebViewX5HostActivity.start(fragment.getContext(), url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTuanDuiData(boolean isRefresh, String shopId, String channel) {
        // teamInfo:工队简介
        // teamPublish:我的发布
        // teamOffer:工种报价
        // teamAchieve:施工业绩
        // teamMember:工队人员
        // teamEquipment:自有设备
        mCurPager = isRefresh ? 1 : mCurPager + 1;
        map.clear();
        map.put("userNo", shopId);
        map.put("channel", channel);
        map.put("pageNum", mCurPager);
        map.put("pageSize", AppConstant.PAGER_LIMIT);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getqueryTeamByUser(map).enqueue(new Callback<BaseEntity<ResponseList<List<ShopTeamInfoEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ResponseList<List<ShopTeamInfoEntity>>>> call, Response<BaseEntity<ResponseList<List<ShopTeamInfoEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    fragment.showEmpty();
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showTuanduiList(response.body().getData(), mCurPager);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                    if (mCurPager <= 1) {
                        fragment.showEmpty();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<ShopTeamInfoEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                if (mCurPager <= 1) {
                    fragment.showEmpty();
                }
            }
        });
    }
}
